//
//  ViewController.m
//  15-UICollectionViewLifeCycle
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define kCollectionViewCellReuse @"reuse"
#define kRandomColor  [UIColor colorWithRed:(arc4random() % 256) / 255.0f  green:(arc4random() % 256) / 255.0f  blue:(arc4random() % 256) / 255.0f  alpha:1]
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (CGRectGetWidth(self.view.frame) - 5) / 2;
    
    layout.itemSize = CGSizeMake(width, 120);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
  
  //关于collectionView LifeCycle
  //1。numberOfSectionsInCollectionView
  //2. numberOfItemsInSection
  //注意：3～5循环执行
  //3.prepareForReuse （从重用池取出时调用）
  //4. collectionView：cellForItemAtIndexPath：
  //5. willDisplayCell
  //6. 当cell完全出屏幕时 didEndDisplayingCell
    
    //iOS 10 以前
  // 出屏幕立即返回， 依旧进入3-》4-》5
  // iOS 10 以后
  // 出屏幕立即返回， 会执行4—》5，不会执行3， 提高了效率
}
#pragma mark - collectionView LifeCycle 
// 区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"[%@ : %s]",  self, __FUNCTION__);
    return 1;
}
// 区里的cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"[%@ : %s]",  self, __FUNCTION__);
    return 100;
}
// 从重用池去取cell
- ( __kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.backgroundColor = kRandomColor;
    NSLog(@"%@----%ld------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
    return cell;
}
// iOS 8.0 以后推出， cell将要显示在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}
// iOS 8.0 以后推出， cell已经离开在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
